package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityFkParamConfig;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoFkParamConfig;
import com.ocom.payconfig.service.FkParamConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class FkParamConfigServiceImpl extends ServiceImpl<IDaoFkParamConfig, EntityFkParamConfig> implements FkParamConfigService {

    @Autowired
    private IDaoFkParamConfig iDaoFkParamConfig;

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public EntityFkParamConfig selectFKParamConfigList(Long comId, Integer merchId) {
        QueryWrapper<EntityFkParamConfig> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",comId);
        queryWrapper.eq("merch_id",merchId);
        return iDaoFkParamConfig.selectOne(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public int saveFkParamConfig(Long comId, EntityFkParamConfig entityFkParamConfigs) {
        Date date=new Date();
        entityFkParamConfigs.setCreateUser(entityFkParamConfigs.getUpdateUser());
        entityFkParamConfigs.setCreateTime(date);
        entityFkParamConfigs.setUpdateTime(date);
        return iDaoFkParamConfig.insert(entityFkParamConfigs);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    @Override
    public int updateFkParamConfig(Long comId, EntityFkParamConfig entityFkParamConfigs) {
        UpdateWrapper<EntityFkParamConfig> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("com_id",comId);
        updateWrapper.eq("merch_id",entityFkParamConfigs.getMerchId());
        /*if (entityFkParamConfigs.getAlltimesOffline() !=null){
            updateWrapper.set("allTimes_offline",entityFkParamConfigs.getAlltimesOffline());
        }if (entityFkParamConfigs.getAllamountOffline() !=null){
            updateWrapper.set("allAmount_offline",entityFkParamConfigs.getAllamountOffline());
        }if (entityFkParamConfigs.getMaxamountOfflin() !=null){
            updateWrapper.set("maxAmount_offlin",entityFkParamConfigs.getMaxamountOfflin());
        }if (entityFkParamConfigs.getMaxtimeOffline() !=null){
            updateWrapper.set("maxTime_offline",entityFkParamConfigs.getMaxtimeOffline());
        }if (entityFkParamConfigs.getOnetimesOffline() !=null){
            updateWrapper.set("oneTimes_offline",entityFkParamConfigs.getOnetimesOffline());
        }if (entityFkParamConfigs.getOneamountOfflin() !=null){
            updateWrapper.set("oneAmount_offlin",entityFkParamConfigs.getOneamountOfflin());
        }if (entityFkParamConfigs.getIsOffline() !=null){
            updateWrapper.set("is_offline",entityFkParamConfigs.getIsOffline());
        }*/
        entityFkParamConfigs.setUpdateTime(new Date());
        return iDaoFkParamConfig.update(entityFkParamConfigs,updateWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyId")
    @Override
    public int deleteFkConfig(Long companyId, Integer merchId) {
        QueryWrapper<EntityFkParamConfig> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("merch_id",merchId);
        return iDaoFkParamConfig.delete(queryWrapper);
    }
}
